package cn.iocoder.yudao.framework.common.genCode.xk.cardPage.behind;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.framework.common.genCode.xk.info.MenuInfo;

import java.util.List;

public class GenCardServiceCodeByJava {


    public String gen(MenuInfo m) {
        String code = new StringBuilderHandler().builder()
                .apAndBl("package " + m.getClassInfo().getPackageName())
                .bl()
                .apAndBl(fragmentImport(m))
                .bl()
                .apAndBl("import com.oneheart.xp.framework.controller.service.impl.AbsCardService;")
                .bl()
                .apAndBl("import com.oneheart.xp.framework.db.dao.AbsEntityDao;")
                .bl()
                .bl()
                .bl()
                .apAndBl("public class " + m.getClassInfo().getCardClassName() + " extends AbsCardService<" + m.getClassInfo().getEntityClassName() + "> {")
                .bl()
                .apAndBl(fragmentDetailFields(m))
                .bl()
                .apAndBl(fragmentReadDetailFromPage(m))
                .bl()
                .apAndBl(fragmentReadDetailFromPage(m))
                .bl()
                .bl()
                .apAndBl(fragmentDoSaveDetail(m))
                .bl()
                .apAndBl(fragmentGetItemDataService(m))
                .bl()
                .apAndBl(fragmentItemDataJson(m))
                .bl()
                .bl()
                .apAndBl("    @Override")
                .apAndBl("    protected AbsEntityDao<" + m.getClassInfo().getEntityClassName() + "> getEntityDao() {")
                .apAndBl("        return new " + m.getClassInfo().getDaoClassName() + "();")
                .apAndBl("    }")
                .bl()
                .apAndBl("    //保存前的校验")
                .apAndBl("    @Override")
                .apAndBl("    protected void checkSave(" + m.getClassInfo().getEntityClassName() + " bean) throws Exception {")
                .apAndBl("        super.checkSave(bean);")
                .apAndBl("    }")
                .bl()
                .apAndBl("    //删除校验")
                .apAndBl("    @Override")
                .apAndBl("    protected void checkDel(" + m.getClassInfo().getEntityClassName() + " bean) throws Exception {")
                .apAndBl("        super.checkDel(bean);")
                .apAndBl("    }")
                .bl()
                .apAndBl("}")
                .toString();

        return code;
    }


    public String fragmentImport(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        fragCode.apAndBl("import com.oneheart.xp.framework.item.EntityEditDetail;");
        for (MenuInfo dM : detailMenuInfos) {
            fragCode.apAndBl(
                    String.format("import %s;", dM.getClassInfo().getPackageName())
            );
        }
        return fragCode.toString();
    }

    /**
     * public EntityEditDetail<YearCalcFirEntity>  yearCalcFir;
     */
    public String fragmentDetailFields(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        for (MenuInfo dM : detailMenuInfos) {
            fragCode.apAndBl(
                    String.format("\"public EntityEditDetail<%s> %s;\""
                            , dM.getClassInfo().getEntityClassName()
                            , dM.getClassInfo().getComponentName())
            );
        }
        return fragCode.toString();
    }

    /**
     *     @Override
     *     protected void readDetailFromPage(PageMap map, CalculateMainEntity bean) throws Exception {
     *         super.readDetailFromPage(map, bean);
     *         JSONObject json = new JSONObject(map.getStr("data"));
     *         yearCalcFir = new EntityEditDetail<>();
     *         yearCalcFir.readFromJson("yearCalcFir", json, new IDetailReadIntf<YearCalcFirEntity>() {
     *             @Override
     *             public YearCalcFirEntity getNewInstance() {
     *                 return new YearCalcFirEntity();
     *             }
     *             @Override
     *             public void doAfterRead(boolean isNew, YearCalcFirEntity det) throws Exception {
     *                 if (isNew) {
     *                     det.setEntityId(PKGenerator.newId());
     *                     det.setNowYear(context.getLoginInfo().getNowyear());
     *                 }
     *                 det.setId(bean.getEntityId());
     *             }
     *         });
     *     }
     */
    public String fragmentReadDetailFromPage(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        fragCode
                .apAndBl("    @Override")
                .apAndBl("    protected void readDetailFromPage(PageMap map, " + m.getClassInfo().getEntityClassName() + " bean) throws Exception {")
                .apAndBl("        super.readDetailFromPage(map, bean);")
                .apAndBl("        JSONObject json = new JSONObject(map.getStr(\"data\"));")
        ;
        for (MenuInfo dM : detailMenuInfos) {
            fragCode
                    .apAndBl("        " + dM.getClassInfo().getComponentName() + " = new EntityEditDetail<>();")
                    .apAndBl("        " + dM.getClassInfo().getComponentName() + ".readFromJson(\"" + dM.getClassInfo().getComponentName() + "\", json, new IDetailReadIntf<" + dM.getClassInfo().getEntityClassName() + ">() {")
                    .apAndBl("            @Override")
                    .apAndBl("            public " + dM.getClassInfo().getEntityClassName() + " getNewInstance() {")
                    .apAndBl("                return new " + dM.getClassInfo().getEntityClassName() + "();")
                    .apAndBl("            }")
                    .apAndBl("            @Override")
                    .apAndBl("            public void doAfterRead(boolean isNew, " + dM.getClassInfo().getEntityClassName() + " det) throws Exception {")
                    .apAndBl("                if (isNew) {")
                    .apAndBl("                    det.setEntityId(PKGenerator.newId());")
                    .apAndBl("                    det.setNowYear(context.getLoginInfo().getNowyear());")
                    .apAndBl("                }")
                    .apAndBl("                det.setId(bean.getEntityId());")
                    .apAndBl("            }")
                    .apAndBl("        });")
            ;

        }
        fragCode
                .apAndBl("    }")
        ;
        return fragCode.toString();
    }

    /**
     *     @Override
     *     protected void doSaveDetail(DfpContext context) throws Exception {
     *         super.doSaveDetail(context);
     *         yearCalcFir.batchSave(new YearCalcFirDao());
     *     }
     *
     */
    public String fragmentDoSaveDetail(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        fragCode
                .apAndBl("    @Override")
                .apAndBl("    protected void doSaveDetail(DfpContext context) throws Exception {")
                .apAndBl("        super.doSaveDetail(context);")
        ;
        for (MenuInfo dM : detailMenuInfos) {
            fragCode
                    .apAndBl("        " + dM.getClassInfo().getComponentName() + ".batchSave(new " + dM.getClassInfo().getDaoClassName() + "());")
            ;

        }
        fragCode
                .apAndBl("    }")
        ;
        return fragCode.toString();
    }

    /**
     *     @Override
     *     public Map<String, AbsListService> getItemDataService() {
     *         Map<String, AbsListService> map = super.getItemDataService();
     *         map.put("yearCalcFir",new YearCalcFirListService());
     *         return map;
     *     }
     */
    public String fragmentGetItemDataService(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        fragCode
                .apAndBl("    @Override")
                .apAndBl("    public Map<String, AbsListService> getItemDataService() {")
                .apAndBl("        Map<String, AbsListService> map = super.getItemDataService();")
        ;
        for (MenuInfo dM : detailMenuInfos) {
            fragCode
                    .apAndBl("        map.put(" + dM.getClassInfo().getComponentName() + ",new YearCalcFirListService());")
            ;

        }
        fragCode
                .apAndBl("        return map;")
                .apAndBl("    }")
        ;
        return fragCode.toString();
    }

    /**
     *     @Override
     *     public void itemDataJson() throws Exception {
     *         super.itemDataJson();
     *         DetailHelper.itemDataJson(this.context, new IDetail() {
     *             public Map<String, AbsListService> getDetailService() {
     *                 return CalculateMainCardService.this.getItemDataService();
     *             }
     *         });
     *     }
     */
    public String fragmentItemDataJson(MenuInfo m) {
        StringBuilderHandler fragCode = new StringBuilderHandler();
        List<MenuInfo> detailMenuInfos = m.getDetailMenuInfos();
        if (detailMenuInfos.size() <= 0) return fragCode.toString();
        fragCode
                .apAndBl("    @Override")
                .apAndBl("    public void itemDataJson() throws Exception {")
                .apAndBl("        super.itemDataJson();")
                .apAndBl("        DetailHelper.itemDataJson(this.context, new IDetail() {")
                .apAndBl("            public Map<String, AbsListService> getDetailService() {")
                .apAndBl("                return "+m.getClassInfo().getCardClassName()+".this.getItemDataService();")
                .apAndBl("            }")
                .apAndBl("        });")
                .apAndBl("    }")
        ;
        return fragCode.toString();
    }

}
